##############################################################
### Table 5. Measurement Model for Latent Variables, China ###
##############################################################

load("/Users/Yannan/Desktop/data_us.RData")

library(lavaan)

### log transformation of monetary transfer variables
log_recode <- function(var) {
  var <- log(var)
  var[var==-Inf] <- 0
  
  return(var)
}
data_china[, 5:12] <- as.data.frame(lapply(data_china[, 5:12], log_recode))

### model3 
model3 <- '
# measurement model
cp13 =~ child_money_support + child_reg_money_support + child_inkind_support + child_reg_inkind_support + see_child_freq + contact_child_freq
pc_past =~ betrothal_gift + betrothal_house + college_support
fct_dis =~ diff_run + diff_sit + diff_climb + diff_stoop + diff_extend + diff_lift + diff_pick + diff_dress + diff_bath + diff_eat + diff_bed + diff_toilet
ses13 =~ living_std_relative + living_std_school + living_std_colleague + living_std_neighbor + living_std_county

# regressions
cp13 ~ pc_past + fct_dis + child_gender + child_income + child_birth_year + child_married + child_education + gender + age + married + ses13
pc_past ~ education + gender + age + married + child_gender + ses13
fct_dis ~ age + ses13 + gender

'

fit <- sem(model3, data = data_china, estimator = "ML", missing = "FIML")
summary(fit, standardized = T)
fitMeasures(fit)


### model 4
model4 <- '
# measurement model
cp13 =~ child_money_support + child_reg_money_support + child_inkind_support + child_reg_inkind_support + see_child_freq + contact_child_freq
pc_past =~ betrothal_gift + betrothal_house + college_support
fct_dis =~ diff_run + diff_sit + diff_climb + diff_stoop + diff_extend + diff_lift + diff_pick + diff_dress + diff_bath + diff_eat + diff_bed + diff_toilet
ses13 =~ living_std_relative + living_std_school + living_std_colleague + living_std_neighbor + living_std_county

# regressions
cp13 ~ pc_past + fct_dis + child_gender + child_income + child_education + age + ses13
pc_past ~ education + gender + age + child_gender + ses13
fct_dis ~ age + ses13 + gender

'

fit2 <- sem(model4, data = data_china, estimator = "ML", missing = "FIML")
summary(fit2, standardized = T)
fitMeasures(fit2)